﻿using System.Configuration;
using MySql.Data.MySqlClient;

namespace footwear
{
    public class DbConnect
    {
        public static MySqlConnection Connection;
        private readonly string _server;
        private readonly string _database;
        private readonly string _uid;
        private readonly string _password;

        public DbConnect()
        {
            _server = "localhost";
            _database = "shoesdatabase";
            if (ConfigurationSettings.AppSettings != null) _uid = ConfigurationSettings.AppSettings["usernameDB"];
            if (ConfigurationSettings.AppSettings != null) _password = ConfigurationSettings.AppSettings["passwordDB"];
            string connectionString = "SERVER=" + _server + ";" + "DATABASE=" +
                                      _database + ";" + "UID=" + _uid + ";" + "PASSWORD=" + _password + ";";
            Connection = new MySqlConnection(connectionString);
        }

        public static bool OpenConnection()
        {
            try
            {
                Connection.Open();
                return true;
            }
            catch (MySqlException)
            {
                return false;
            }
        }

        public static bool CloseConnection()
        {
            try
            {
                Connection.Close();
                return true;
            }
            catch (MySqlException)
            {
                return false;
            }
        }
    }
}